﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Linq;
using System.Web.Services;
using System.Data.Objects.DataClasses;
using System.Web.Mail;
using System.Net.Mail;
using System.Net;
using System.Collections;

namespace WebService
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    public class Service1 : System.Web.Services.WebService
    {
        //-------------------------------------------chauffeur--------------------------------------------------------------------------
        //permet de verifier l'existance  d'un chauffeur


        [WebMethod]
        public Chauffeur ConnxionChauffeur(string log, string pwd)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var chauff = (from x in cxt.Chauffeur where ((x.Login == log) && (x.Password == pwd)) select x).FirstOrDefault();
                if (chauff != null)
                    return chauff;
                else
                    return null;
            }
        }
        //permet d'ajouter un chauffeur
        [WebMethod]
        public bool AjouterChauffeur(string n, string p, string a, string t, string e, string i, string l, string pwd)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var chauff = new Chauffeur { Nom = n, Prenom = p, Adresse = a, Tel = t, Email = e, Image = i, Login = l, Password = pwd };
                cxt.AddToChauffeur(chauff);
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //permet de suprimer un chauffeur à l'aide de son id
        [WebMethod]
        public bool SupprimerChauffeur(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unchauff in cxt.Chauffeur
                              where (unchauff.id_chauffeur == id)
                              select unchauff).First();
                if (result != null)
                {
                    cxt.DeleteObject(result);
                }

                if (cxt.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        //permet de modifier un chauffeur
        [WebMethod]
        public bool ModifierChauffeur(string n, string p, string a, string t, string l, string pass, string i, string e, int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unchauff in cxt.Chauffeur where (unchauff.id_chauffeur == id) select unchauff).First();
                if (result != null)
                {
                    result.Nom = n;
                    result.Prenom = p;
                    result.Adresse = a;
                    result.Tel = t;
                    result.Login = l;
                    result.Password = pass;
                    result.Image = i;
                    result.Email = e;
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //permet de consulter un chaffeur
        [WebMethod]
        public Chauffeur ConsulterChauffeur(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from chauff in cxt.Chauffeur
                              where chauff.id_chauffeur == id
                              select chauff).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        //permet d'afficher la liste des chaffeurs
        [WebMethod]
        public List<Chauffeur> AfficherListeChauffeur()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from leschauff in cxt.Chauffeur select leschauff).ToList<Chauffeur>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }


        //-------------------------------------------employee--------------------------------------------------------------------------


        //permet de verifier l'existance  d'un employee


        [WebMethod]
        public Employer Connxionemployee(string log, string pwd,int type)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var empl = (from x in cxt.Employer where ((x.Login == log) && (x.Password == pwd)&&(x.Type==type))
                            select x).FirstOrDefault();
                if (empl != null)
                    return empl;
                else
                    return null;
            }
        }

        //permet d'ajouter un employee
        [WebMethod]
        public bool Ajouteremployee(string n, string p, string a, string t, string e, string i, string l, string pwd, int ty)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var employ = new Employer { Nom = n, Prenom = p, Adresse = a, Tel = t, Email = e, Image = i, Login = l, Password = pwd, Type = ty };
                cxt.AddToEmployer(employ);
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //permet de suprimer un employee à l'aide de son id
        [WebMethod]
        public bool SupprimerEmployer(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unemploy in cxt.Employer
                              where (unemploy.id_employer == id)
                              select unemploy).First();
                if (result != null)
                {
                    cxt.DeleteObject(result);
                }

                if (cxt.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        //permet de modifier un employee
        [WebMethod]
        public bool ModifierEmployer(string n, string p, string a, string t, string l, string pass, string i, string e, int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unemploy in cxt.Employer where (unemploy.id_employer == id) select unemploy).First();
                if (result != null)
                {
                    result.Nom = n;
                    result.Prenom = p;
                    result.Adresse = a;
                    result.Tel = t;
                    result.Login = l;
                    result.Password = pass;
                    result.Image = i;
                    result.Email = e;
                    
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //permet de consulter un employee
        [WebMethod]
        public Employer ConsulterEmployer(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from Employ in cxt.Employer
                              where Employ.id_employer == id
                              select Employ).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        //permet d'afficher la liste des employees
        [WebMethod]
        public List<Employer> AfficherListeEmployer()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from lesEmployers in cxt.Employer select lesEmployers).ToList<Employer>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }



        //-------------------------------------------Vehicule--------------------------------------------------------------------------




        //permet d'ajouter un Vehicule
        [WebMethod]
        public bool AjouterVehicule(string mat, string mar)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var veh = new Vehicule { Matricule = mat, Marque = mar };
                cxt.AddToVehicule(veh);
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //permet de suprimer un Vehicule à l'aide de son id
        [WebMethod]
        public bool SupprimerVehicule(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unveh in cxt.Vehicule
                              where (unveh.id_vehicule == id)
                              select unveh).First();
                if (result != null)
                {
                    cxt.DeleteObject(result);
                }

                if (cxt.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        //permet de modifier un Vehicule
        [WebMethod]
        public bool ModifierVehicule(string mat, string mar, int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unveh in cxt.Vehicule where (unveh.id_vehicule == id) select unveh).First();
                if (result != null)
                {
                    result.Marque = mar;
                    result.Matricule = mat;


                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //permet de consulter un Vehicule
        [WebMethod]
        public Vehicule ConsulterVehicule(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from veh in cxt.Vehicule
                              where veh.id_vehicule == id
                              select veh).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        //permet d'afficher la liste des véhicules
        [WebMethod]
        public List<Vehicule> AfficherListeVehicule()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from lesVehicules in cxt.Vehicule select lesVehicules).ToList<Vehicule>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }


        //-------------------------------MISSION-------------------------------------------

        //permet d'avoir la liste des point d'une route apartir de l'id d'une mission
        [WebMethod]
        public List<Point> ConsulterPointsRouteMission(int idmission)
        {
            //var result = (from lespositions in cxt.Point select lespositions).ToList<Position>();
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from uneMission in cxt.Mission
                              where uneMission.id_mission == idmission
                              select uneMission).First();


                var result2 = (from lespoints in cxt.Point
                               where lespoints.id_route == result.id_mission
                               select lespoints).ToList<Point>();

                if (result2 != null)
                    return result2;
                else
                    return null;
            }
        }


        //donner le nombre des missions d'un chaufeur
        [WebMethod]
        public int CountMissionsEnCoursByChauffeur(int idchauff)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = from uneMission in cxt.Mission
                             where uneMission.id_chauffeur == idchauff
                             && uneMission.Etat.Equals("En Cours")
                             select uneMission;
                if (result != null)
                    return result.Count();
                else
                    return 0;
            }
        }

        //permet de modifier l'etat d'une mission
        [WebMethod]
        public bool Modif_Etat_Mission(int idm, String etat)
        {
            //DateTime Dat = Convert.ToDateTime(Dat_f_r); 
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unMiss in cxt.Mission where (unMiss.id_mission == idm) select unMiss).First();
                if (result != null)
                {
                    result.Etat = etat;
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        
        //permet d'ajouter une mission
        [WebMethod]
        public bool AjouterMission(string l, int id_chauff, int id_rt, int veh, DateTime dat_dec, DateTime dat_f_est)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var tt = (from xx in cxt.Trajectoire
                          select xx.id_route).Max();
                DateTime dat_1_notif = dat_dec.AddDays(-1);// avant un jour
                DateTime dat_2_notif = dat_f_est.AddHours(-2); // avant 2 heures
                var miss = new Mission
                {
                    libellé = l,
                    Etat = "En attente",
                    id_chauffeur = id_chauff,
                    id_route = tt,
                    id_vehicule = veh,
                    DateDeclanchement = dat_dec,
                    DateFinEstimé = dat_f_est,
                    DatePremierNotif = dat_1_notif,
                    DateDeuxiemeNotif = dat_2_notif  
                };

                cxt.AddToMission(miss);
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }


        //permet d'afficher la liste des missions 
        [WebMethod]
        public List<Mission> AfficherListeMissions()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var res = (from Miss in cxt.Mission select Miss).ToList<Mission>();
                if (res != null)
                    return res;
                else
                    return null;
            }
        }

        //permet de consulter une mission 
        [WebMethod]
        public Mission ConsulterMission(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from miss in cxt.Mission
                              where miss.id_mission == id
                              select miss).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }

     


        //permet de suprimer une mission à l'aide de son id
        [WebMethod]
        public bool SupprimerMission(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unMiss in cxt.Mission
                              where (unMiss.id_mission == id)
                              select unMiss).First();
                if (result != null)
                {
                    cxt.DeleteObject(result);
                }

                if (cxt.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }
        //permet de modifier une mission
        [WebMethod]
        public bool Modifier_Mission(int id, int id_chauff, int id_r, int veh, string dat_d_r, string dat_f_r, string Et)
        {
            DateTime Dat = Convert.ToDateTime(dat_d_r);
            DateTime Dat2 = Convert.ToDateTime(dat_f_r);
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unMiss in cxt.Mission where (unMiss.id_mission == id) select unMiss).First();
                if (result != null)
                {

                    result.DateDebutReelle = Dat;
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }

        //permet de cloturer une mission
        [WebMethod]
        public bool Cloturer_Mission(int id)
        {
            
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unMiss in cxt.Mission where (unMiss.id_mission == id) select unMiss).First();
                if (result != null)
                {

                    result.DateFinReelle = DateTime.Now;
                    result.Etat = "Cloturée";
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }



        //donner l'id mission en cours du chauffeur dont l'id est passé en parametre
        [WebMethod]
        public int AfficherIdMissionsEnCoursByChauffeur(int idchauff)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from uneMission in cxt.Mission
                              where uneMission.id_chauffeur == idchauff
                              && uneMission.Etat.Equals("En Cours")
                              select uneMission).First();
                if (result != null)
                    return result.id_mission;
                else
                    return 0;
            }
        }

        //permet de declencher une mission
        [WebMethod]
        public bool Declancher_Mission(int id)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unMiss in cxt.Mission where (unMiss.id_mission == id) select unMiss).First();
                if (result != null)
                {

                    result.DateDebutReelle = DateTime.Now;
                    result.Etat = "En Cours";
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }

        //afficher la liste des missions d'un chauffeur enattente
        [WebMethod]
        public List<Mission> AfficherListeMissionChauffeurEnattente(int idch)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unemiss in cxt.Mission
                              where (unemiss.id_chauffeur == idch && unemiss.Etat == "En attente")
                              select unemiss).ToList<Mission>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }

        //afficher la liste des missions d'un chauffeur enattente
        [WebMethod]
        public List<Mission> AfficherListeMissionChauffeurCloturée(int idch)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unemiss in cxt.Mission
                              where (unemiss.id_chauffeur == idch && unemiss.Etat == "Cloturée")
                              select unemiss).ToList<Mission>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        // permer d'envoyer un mail
        [WebMethod]
        public void envoyer_email(string from, string objet, string to, string msgbody)
        {
            // Objet mail
            using (System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage())
            {
                // Expéditeur (obligatoire). Notez qu'on peut spécifier le nom
                msg.From = new MailAddress(from);
                // Destinataires (il en faut au moins un)
                msg.To.Add(new MailAddress(to));
                msg.Subject = objet;
                //msg.To.Add(new MailAddress("agent.smith@matrix.com", "Agent Smith"));
                // Destinataire(s) en copie (facultatif)
                //msg.Cc.Add(new MailAddress("wonder.woman@superhero.com", "Wonder Woman"));
                // Texte du mail (facultatif)
                msg.Body = msgbody;
                // Fichier joint si besoin (facultatif)
                //msg.Attachments.Add(new Attachment(@"c:\fichierjoint.txt"));
                // Envoi du message SMTP
                using (SmtpClient client = new SmtpClient("smtp.gmail.com", 587))
                {
                    client.EnableSsl = true;
                    client.Credentials = new NetworkCredential("ssvgps2012@gmail.com", "Projet2012");
                    // Envoi du mail
                    client.Send(msg);
                }
            }

            
        }

        //*********************************************Messages***************************************//

        //permet d'envoyer un message

        [WebMethod]
        public bool envoyerMsg(string txt, int id_ch)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var msg = new Message {Text = txt, id_chauffeur =id_ch, Etat="En attente"  };
                cxt.AddToMessage(msg);
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }

        // permet d'afficher un message

        [WebMethod]
        public Message afficherMsg(int idmsg)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unmsg in cxt.Message
                              where (unmsg.id_message == idmsg)
                              select unmsg).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }


        // permet d'afficher liste des messages


        [WebMethod]
        public List<Message> AfficherListeMsg()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unmsg in cxt.Message
                              select unmsg).ToList<Message>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }

        // permet de lire un message


        [WebMethod]
        public bool lireMsg(int idmsg)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unmsg in cxt.Message where (unmsg.id_message == idmsg) select unmsg).FirstOrDefault();
                if (result != null)
                {
                    result.Etat = "Envoyé";
                    //result.id_chauffeur = idch;
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }

        //afficher la liste des messages d'un chauffeur
        [WebMethod]
        public List<Message> AfficherListeMsgChauffeur(int idch)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unmsg in cxt.Message
                              where unmsg.id_chauffeur == idch
                              select unmsg).ToList<Message>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }

        //afficher la liste des messages d'un chauffeur envoyé
        [WebMethod]
        public List<Message> AfficherListeMsgChauffeurEnvoyé(int idch)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unmsg in cxt.Message
                              where (unmsg.id_chauffeur == idch && unmsg.Etat == "Envoyé")
                              select unmsg).ToList<Message>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }

        //afficher la liste des messages d'un chauffeur enattente
        [WebMethod]
        public List<Message> AfficherListeMsgChauffeurEnattente(int idch)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unmsg in cxt.Message
                              where (unmsg.id_chauffeur == idch && unmsg.Etat == "En attente")
                              select unmsg).ToList<Message>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }

        //permet de supprimer un message
        [WebMethod]
        public bool SupprimerMsg(int idmsg)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unmsg in cxt.Message
                              where (unmsg.id_message == idmsg)
                              select unmsg).FirstOrDefault();
                if (result != null)
                {
                    cxt.DeleteObject(result);
                }

                if (cxt.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }


        //**********************************************Notifications****************************************//
        //permet d'envoyer une notification à un admin

        [WebMethod]
        public bool envoyerNoti(string txt, int id_ch,int type)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var noti = new Notification {Text = txt, id_chauffeur = id_ch ,Etat=0,type=type};
                cxt.AddToNotification(noti);
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }

        //permet d'envoyer une notification à un chauff

        [WebMethod]
        public bool envoyerNoti_Msg(string txt, int id_ch, int type)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var noti = new Notification { Text = txt, id_chauffeur = id_ch, Etat = 1, type = type };
                cxt.AddToNotification(noti);
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }

        // permet d'afficher un notification

        [WebMethod]
        public Notification afficherNoti(int idnoti)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unnoti in cxt.Notification
                              where (unnoti.id_notifaication == idnoti)
                              select unnoti).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }

       
        // permet d'afficher liste des notifications


        [WebMethod]
        public List<Notification> AfficherListeNoti()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unnoti in cxt.Notification
                              where unnoti.type==0
                              select unnoti).ToList<Notification>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        // permet d'afficher liste des notifications non lu(3adya)


        [WebMethod]
        public List<Notification> AfficherListeNotiNonLu()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unnoti in cxt.Notification
                              where (unnoti.Etat==0 && unnoti.type==0)
                              select unnoti).ToList<Notification>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        // permet d'afficher liste des notifications non lu(message)
        [WebMethod]
        public List<Notification> AfficherListeNotiNonLu_msg()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unnoti in cxt.Notification
                              where (unnoti.Etat == 0 && unnoti.type == 1)
                              select unnoti).ToList<Notification>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        // permet de lire une notification(3adya)


        [WebMethod]
        public bool lireNoti(int idnoti)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unnoti in cxt.Notification where (unnoti.id_notifaication == idnoti && unnoti.type==0) select unnoti).FirstOrDefault();
                if (result != null)
                {
                    result.Etat = 1;
                   
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }

        // permet de lire une notification(msg)


        [WebMethod]
        public bool lireNoti_msg(int idnoti)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unnoti in cxt.Notification where (unnoti.id_notifaication == idnoti && unnoti.type == 1) select unnoti).FirstOrDefault();
                if (result != null)
                {
                    result.Etat = 1;

                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //afficher la liste des notifications d'un chauffeur(3adya)
        [WebMethod]
        public List<Notification> AfficherListeNotiChauffeur(int idch)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unnoti in cxt.Notification
                              where unnoti.id_chauffeur == idch && unnoti.type==0
                              select unnoti).ToList<Notification>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }

        //afficher la liste des notifications d'un chauffeur(msg)
        [WebMethod]
        public List<Notification> AfficherListeNotiChauffeur_msg(int idch)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unnoti in cxt.Notification
                              where unnoti.id_chauffeur == idch && unnoti.type==1
                              select unnoti).ToList<Notification>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }

        // permet d'afficher liste des notifications non lu d'un chauffeur(message)
        [WebMethod]
        public List<Notification> AfficherListeNotiNonLuCH_msg(int idch)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unnoti in cxt.Notification
                              where (unnoti.id_chauffeur == idch && unnoti.Etat == 0 && unnoti.type == 1)
                              select unnoti).ToList<Notification>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }


        //permet de supprimer un notification
        [WebMethod]
        public bool SupprimerNoti(int idnoti)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from unnoti in cxt.Notification
                              where (unnoti.id_notifaication == idnoti)
                              select unnoti).FirstOrDefault();
                if (result != null)
                {
                    cxt.DeleteObject(result);
                }

                if (cxt.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }

        //**************************************************tache**************************************//
        //permet d'ajouter une tache
        [WebMethod]
        public bool AjouterTache(int idtache, string nom, string adresse, int id_m, int id_poi)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var tache = new Tache { id_tache = idtache,Nom= nom, Adresse= adresse, id_mission= id_m, id_point= id_poi, Etat="En attente" };

               cxt.AddToTache(tache);
                  if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
            }
        //permet de supprimer une tache 
        [WebMethod]
                            
        public bool Supprimertache(int idtache)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from untache in cxt.Tache
                              where (untache.id_tache == idtache)
                              select untache).FirstOrDefault();
                if (result != null)
                {
                    cxt.DeleteObject(result);
                }

                if (cxt.SaveChanges() > 0)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

        }
        //permet de consulter une tache
        [WebMethod]
        public Tache Consultertache(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from untache in cxt.Tache
                              where (untache.id_tache == id)
                              select untache).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
       
        //afficher la liste des taches
        [WebMethod]
        public List<Tache> AfficherListeTaches()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from lestaches in cxt.Tache select lestaches).ToList<Tache>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        //donner le nombre des taches
        [WebMethod]
        public int CountTaches()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from untache in cxt.Tache
                              select untache).Distinct();
                if (result != null)
                    return result.Count();
                else
                    return 0;
            }
        }

        // permet de valider tache
        [WebMethod]
        public bool ValiderTache(int idtache, int id_m, int id_p)
        {
            
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from untache in cxt.Tache where (untache.id_tache == idtache) select untache).FirstOrDefault();
                if (result != null)
                {
                    result.Etat = "Validé";
                    result.id_mission = id_m;
                    result.id_point = id_p;
                }
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //recuperer la position des chauffeurs dans les 10 min dernier 
        [WebMethod]
        public List<Position> RecupererPosition()
        { 
        using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
            //
            //var result1=from pos in cxt.Position
            //            group pos by (pos.id_chauffeur) into ps
            //            select ps;


                DateTime d = DateTime.Now.AddMinutes(-10);
                var result = (from lesposition in cxt.Position
                           //   where (d.CompareTo(lesposition.Date) < 0)
                              orderby lesposition.id_position descending
                              select lesposition).ToList<Position>();

                if (result.Count()>0)
                    return result;
                else
                    return null;
            }
        }
        //permet de consulter une Trajectoire
        [WebMethod]
        public Trajectoire ConsulterTrajectoire(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from traj in cxt.Trajectoire
                              where traj.id_route == id
                              select traj).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        //permet de consulter un point
        [WebMethod]
        public Point ConsulterPoint(int id)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from pt in cxt.Point
                              where pt.id_point == id
                              select pt).FirstOrDefault();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
        //permet d'ajouter un trajectoire
        [WebMethod]
        public bool AjouterTrajectoire(Trajectoire traj)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {

                cxt.AddToTrajectoire(traj);
                if (cxt.SaveChanges()>0)
                    return true;
                else
                    return false;
            }
        }
        //permet d'ajouter une liste des points
        [WebMethod]
        public bool AjouterListePoint(List<Point> lespt)
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var tt = (from xx in cxt.Trajectoire
                          select xx.id_route).Max();
                foreach (var xx in lespt)
                {
                    Point pt = new Point {Lat=xx.Lat,Lon=xx.Lon,id_route=tt };
                    cxt.AddToPoint(pt);
                }
                
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;
            }
        }
        //pour ajouter position
        [WebMethod]
        public bool AjouterPositionTest(float vla, float vlo, int idchauffeur)
        {

            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                DateTime date1 = DateTime.UtcNow;
                float la = (float)vla;
                float lo = (float)vlo;
                var result = (from chauff in cxt.Chauffeur
                              where chauff.id_chauffeur == idchauffeur
                              select chauff).FirstOrDefault();
                if (result == null)
                    return false;

                var t = new Position { Lat = la, Lon = lo, Date = date1, Chauffeur = result };
                cxt.AddToPosition(t);
                if (cxt.SaveChanges() > 0)
                    return true;
                else
                    return false;

            }
        }

        //permet d'afficher la liste des positions
        [WebMethod]
        public List<Position> AfficherListePosition()
        {
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from lespositions in cxt.Position select lespositions).ToList<Position>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
     
        //permet d'avoir la liste des point d'une route
        [WebMethod]
        public List<Point> ConsulterPointsRoute(int id)
        {
            //var result = (from lespositions in cxt.Point select lespositions).ToList<Position>();
            using (databasessvgpsEntities cxt = new databasessvgpsEntities())
            {
                var result = (from lespoints in cxt.Point
                              where lespoints.id_route == id
                              select lespoints).ToList<Point>();
                if (result != null)
                    return result;
                else
                    return null;
            }
        }
    

    }
}